package main

import (
	"context"
	"fmt"
	protoExample "golang-grpc-example/server/proto"
	"google.golang.org/grpc"
	"net"
)

type server struct {
	protoExample.UnimplementedExampleServer
}

func (s *server) ExampleFunc(ctx context.Context, req *protoExample.ExampleRequest) (*protoExample.ExampleResponse, error) {
	fmt.Println("request " + req.RequestName + " received!")
	return &protoExample.ExampleResponse{ResponseMsg: "HuangHeng hello world! + " + req.RequestName}, nil
}

func main() {
	// 开启端口
	listen, _ := net.Listen("tcp", "127.0.0.1:9090")
	// 创建grpc服务端
	grpcServer := grpc.NewServer()
	// 注册服务
	protoExample.RegisterExampleServer(grpcServer, &server{})
	// 启动服务
	err := grpcServer.Serve(listen)
	if err != nil {
		fmt.Printf("fail to serve: %v\n", err)
		return
	}
}
